package com.lzy.shops.service.impl;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.lzy.shops.bean.Area;
import com.lzy.shops.bean.UserAddress;
import com.lzy.shops.bean.UserAndAddress;
import com.lzy.shops.mapper.UserAndAddressMapper;
import com.lzy.shops.service.UserAndAddressService;
import com.lzy.shops.utils.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class UserAndAddressServiceImpl implements UserAndAddressService {

    @Autowired
    private UserAndAddressMapper mapper;

    @Override
    public UserAndAddress findByUser(Integer userId,Integer start,Integer end) {
        return mapper.findByUser(userId,start,end);
    }

    @Override
    public Integer findByUserCount(Integer userId) {
        return mapper.findByUserCount(userId);
    }

    @Override
    @Transactional
    public Boolean insertUserAddress(Integer userId,UserAddress userAddress){
        ResponseData<Object> responseData = null;
        try {
            Integer integer = mapper.insertUserAddress(userAddress);
            if(integer!=0&&integer!=null){
                Integer row = mapper.insertUserAndAddress(userAddress.getId().intValue(), userId);
                if(row!=0&&row!=null){
                    responseData = new ResponseData<Object>(true,"新增成功");
                    return true;
                }
            }
        } catch (Exception e) {
            throw new RuntimeException("数据异常");
        }
        return false;
    }

    @Override
    @Transactional
    public Boolean delUserAddress(Integer addressId, Integer userId) {
        try {
            Integer integer = mapper.delUserAndAddress(addressId, userId);
            if(integer!=null&&integer!=0){
                Integer row = mapper.delUserAddress(addressId);
                if(row!=null&&row!=0){
                    return true;
                } throw new RuntimeException("数据异常");
            } throw new RuntimeException("数据异常");
        } catch (Exception e) {
            return false;
        }
    }
}
